<template>
  <div class="coupon">
    <coupon-content
      v-for="item in list"
      :key="item.id"
      :couponContet="item"
      :location="location"
    />
  </div>
</template>

<script>
import { shelfList } from "@/service/api/sp-coupon";
import couponContent from "@/components/pageComponents/sp-coupon/coupon-content";
import { pubAnonShelfList } from "@/service/api/sp-store/wifi";
import dayjs from "dayjs";

export default {
  components: { couponContent },
  data() {
    return {
      params: {
        latitude: "",
        longitude: "",
        shopName: "",
        shopType: "2",
        sortType: 1,
        pageNum: 1,
        pageSize: 10,
      },
      list: [],
      total: 0,
      fetchDataNum: 0,
    };
  },
  created() {},
  mounted() {
    this.$getUserLocation().then((res) => {
      let result = res?.currentLocation ? res?.currentLocation : res?.location;
      let { longitude, latitude } = result;
      this.params.longitude = longitude;
      this.params.latitude = latitude;
      this.$nextTick(() => {
        this.fetch();
      });
    });
  },
  methods: {
    async fetch() {
      try {
        this.fetchDataNum += 1;
        const { pageNum } = this.params;
        const res = await pubAnonShelfList({ ...this.params });
        let { total } = res;
        this.total = total;
        if (res) {
          let list = this.toUpperCaseFunc(res.list);
          list = this.expireAfterFunc(list);
          this.list =
            pageNum === 1
              ? this.parseCouponList(list)
              : this.list.concat(this.parseCouponList(list));
        }
      } catch (e) {
        setTimeout(() => {
          if (this.fetchDataNum >= 10) return;
          this.fetch();
        }, 1000);
      }
    },
    //计算即将过期的券
    parseCouponList(list) {
      list = list.map((value) => {
        let customerCoupon = value;
        let nowTime = dayjs();
        let day = dayjs(customerCoupon.expireTime).diff(nowTime, "day");
        let effective = dayjs(customerCoupon.effectiveTime).diff(
          nowTime,
          "day"
        );
        if (day === 0) {
          value.expireTag = "今天即将过期";
        }
        if (day < 0) {
          value.expireTag = "已过期";
        }
        value.day = day;
        value.effective = effective;
        value.dillExpireTime = dayjs(customerCoupon.expireTime)
          .subtract(1, "second")
          .format("YYYY-MM-DD");
        value.effectiveTime = dayjs(customerCoupon.effectiveTime).format(
          "YYYY-MM-DD"
        );
        if (customerCoupon.couponType === "CASH") {
          let reduceCost = customerCoupon.reduceCost / 100;
          let reduceCostArr = reduceCost.toString().split(".");
          value.balanceBefore = reduceCostArr[0];
          if (reduceCostArr.length > 1) {
            value.afterBalance = reduceCostArr[1];
          }
        } else if (customerCoupon.couponType === "DISCOUNT") {
          //处理折扣字段
          let reduceDiscount = customerCoupon.discount / 10;
          let reduceDiscountArr = reduceDiscount.toString().split(".");
          if (reduceDiscountArr.length > 1) {
            value.discountBefore = reduceDiscountArr[0];
            value.discountAfter = reduceDiscountArr[1];
          } else {
            value.discountBefore = reduceDiscountArr[0];
            value.discountAfter = 0;
          }
        }
        return value;
      });
      return list;
    },
    expireAfterFunc(list) {
      let day1 = Date.parse(new Date());
      let arr = [];
      if (list.length > 0) {
        list.forEach((item) => {
          if (item) {
            let expireTime = item.expireTime;
            let day2 = new Date(expireTime.replace(/-/g, "/")).getTime();
            item.after = (day2 - day1) / (1000 * 60 * 60 * 24);
            if (item.after < 30) {
              arr.push(item);
            }
          }
        });
        // this.setData({
        //   pastCouponList: arr,
        // });
        return list;
      }

      return [];
    },
    //兼容新 旧券 大小写和类型问题

    toUpperCaseFunc(list) {
      if (list.length > 0) {
        let str = /^8128\d{16,20}$/;
        list.forEach((item) => {
          let type = item.couponType;
          item.type = type;
          // if (
          //   type === "cash" ||
          //   type === "cash_wx_busi" ||
          //   type === "cash_wx_pay"
          // ) {
          //   item.couponType = "CASH";
          // } else if (type === "discount" || type === "discount_wx_busi") {
          //   item.couponType = "DISCOUNT";
          // } else if (type === "exchange") {
          //   item.couponType = "GENERAL";
          // }
          // item.couponType = item.couponType.toUpperCase();
        });
        return list;
      } else {
        return [];
      }
    },
  },
};
</script>

<style scoped lang="scss">
.coupon {
  padding: 20rpx;
}
</style>
